package com.wfy.controller;

import com.sun.org.apache.regexp.internal.RE;
import com.wfy.pojo.Brand;
import com.wfy.pojo.PageResult;
import com.wfy.pojo.Sku;
import com.wfy.pojo.TbSpuDetail;
import com.wfy.pojo.vo.SpuVo;
import com.wfy.service.BrandService;
import com.wfy.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: GoodsController
 * @Package com.wfy.controller
 * @Author 王飞遥
 * @Date 2022/8/24 20:44
 */
@RestController
public class GoodsController {
    @Autowired
    GoodsService goodsService;
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuVo>> querySpuVoPage(
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "saleable",required = false) Boolean saleable,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows
    ){
         PageResult<SpuVo> pageList=goodsService.querySpuVoPage(key,saleable,page,rows);
            if(CollectionUtils.isEmpty(pageList.getItems())){
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok(pageList);
    }
    @PostMapping("/goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuVo spuVo){
        goodsService.saveGoods(spuVo);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    @GetMapping("/spu/detail/{spuId}")
    public ResponseEntity<TbSpuDetail> queryDetailBySpuId(@PathVariable("spuId")Long spuId){
        TbSpuDetail spuDetail= goodsService.queryDetailBySpuId(spuId);
        return ResponseEntity.ok(spuDetail);
    }
    @GetMapping("/sku/list")
    public ResponseEntity<List<Sku>> querySkuListBySpuId(@RequestParam("id")Long id){
    List<Sku> list=goodsService.querySkuListBySpuId(id);
    if(CollectionUtils.isEmpty(list)){
        return ResponseEntity.notFound().build();
    }
    return ResponseEntity.ok(list);
    }
    @PutMapping("/goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuVo spuVo){
        goodsService.updateGoods(spuVo);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}
